<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@ taglib uri="/WEB-INF/lib/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/lib/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/lib/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/lib/vSys.tld" prefix="vsys"%>
<%@ taglib uri="/WEB-INF/lib/c.tld" prefix="c"%>


<script>
function submitForm(opr){
	document.vehicleForm.opr.value = opr;
	document.vehicleForm.submit();
//	alert("teste");
}

function insertVehicle(){
	document.vehicleForm.opr.value = 'insert';
	if (confirm('<bean:message key="vehicle.insert.confirmationPrompt" />')){
		document.vehicleForm.submit();
	}
}

function editVehicle(vehicleId){
	document.vehicleForm.vehicleId.value = vehicleId;
	document.vehicleForm.opr.value = 'changeToEditMode';
	document.vehicleForm.submit();
}

function saveVehicle(){
	document.vehicleForm.opr.value = 'edit';
	if (confirm('<bean:message key="vehicle.edit.confirmationPrompt" />')){
		document.vehicleForm.submit();
	}
}

function removeVehicle(vehicleId){
	document.vehicleForm.opr.value = 'remove';
	document.vehicleForm.vehicleId.value = vehicleId;
	if (confirm('<bean:message key="vehicle.remove.confirmationPrompt" />')){
		document.vehicleForm.submit();
	}
}

function clearFields(){

	document.vehicleForm.opr.value = 'clearFields';
	document.vehicleForm.submit();
}

</script>

<div style="font-weight: bold;">
	<vsys:showErrors />
	<br/>
</div>

<html:form action="/vehicle">
	<html:hidden property="opr" />
	<html:hidden property="vehicleId" />


	<table border="0" width="100%">
		<tr>
			<td>
			<table border="0" width="100%">
				<tr>
					<td width="40%"><bean:message key="vehicle.labels.plate" />:<br />
					<html:text property="plate" /></td>
					<td width="30%"><bean:message key="vehicle.labels.year" />:<br />
					<html:text property="year" size="4" maxlength="4" /></td>
					<td width="30%"><bean:message key="vehicle.labels.value" />:<br />
					<html:text property="value" size="10" maxlength="10" /></td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td>
			<table width="100%">
				<tr>
					<td width="80%"><bean:message key="vehicle.labels.city" />:<br />
					<html:text property="city" size="50" maxlength="50" /></td>
					<td width="20%"><bean:message key="vehicle.labels.state" />:<br />
					<html:text property="state" size="2" maxlength="2" /></td>
				</tr>

			</table>
			</td>
		</tr>
		<tr>
			<td>
			<table width="100%">
				<tr>
					<td width="25%"><bean:message key="vehicle.labels.brand" />:<br />
					<html:select property="selectedBrand"
						onchange="submitForm('changeModel')">
						<html:options collection="brandsCollection" property="id"
							labelProperty="brand" />
					</html:select></td>
					<td width="25%"><bean:message key="vehicle.labels.model" />:<br />
					<html:select property="selectedModel">
						<html:options collection="modelsCollection" property="id"
							labelProperty="model" />
					</html:select></td>
					<td width="25%"><bean:message key="vehicle.labels.fuel" />:<br />
					<html:select property="selectedFuel">
						<html:options collection="fuelsCollection" property="id"
							labelProperty="fuel" />
					</html:select></td>
					<td width="25%"><bean:message key="vehicle.labels.color" />:<br />
					<html:select property="selectedColor">
						<html:options collection="colorsCollection" property="id"
							labelProperty="color" />
					</html:select></td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
		 <td>
			<table width="100%">
				<tr>
					<td width="30%">
					</td>
					<td width="30%">

						<c:if test="${empty vehicleForm.vehicleId}">
							<a href="javascript:insertVehicle()"><bean:message key="vehicle.labels.insertButton" /></a>
						</c:if>
						<c:if test="${not empty vehicleForm.vehicleId}">
							<a href="javascript:saveVehicle()"><bean:message key="vehicle.labels.saveButton" /></a>
						</c:if>


					</td>
					<td width="20%">
						<a href="javascript:clearFields()"><bean:message key="vehicle.labels.clearButton" /></a>
					</td>
					<td width="30%">
					</td>
				</tr>
			</table>
		 </td>
		</tr>
	</table>

<br/>
<br/>

<table width="100%" style="border: 1px solid #969696;" cellpadding="0" cellspacing="0">
	<tr style="background-color: #969696; height: 20px; font-weight: bold;">
		<td>&nbsp;</td>
		<td width="15%"><bean:message key="vehicle.labels.plate" /></td>
		<td width="10%"><bean:message key="vehicle.labels.year" /></td>
		<td width="15%"><bean:message key="vehicle.labels.value" /></td>
		<td width="10%"><bean:message key="vehicle.labels.brand" /></td>
		<td width="10%"><bean:message key="vehicle.labels.model" /></td>
		<td width="10%"><bean:message key="vehicle.labels.color" /></td>
		<td width="15%"><bean:message key="vehicle.labels.fuel" /></td>
		<td width="7%"></td>
		<td width="5%"></td>
		<td>&nbsp;</td>
	</tr>
	
	
	<logic:iterate id="vehicle" name="vehiclesCollection" >

	<c:choose>
		<c:when test="${color == '#f3f2f2'}">
			<c:set var="color" value="#e7e6e6" scope="page"/>
		</c:when>
		<c:otherwise>
			<c:set var="color" value="#f3f2f2" scope="page"/>
		</c:otherwise>
	</c:choose>
	
		<tr style="background-color: <c:out value="${color}" />; height: 15px;">
			<td>&nbsp;</td>
			<td><bean:write name="vehicle" property="plate"/></td>
			<td><bean:write name="vehicle" property="year"/></td>
			<td><bean:write name="vehicle" property="value"/></td>
			<td><bean:write name="vehicle" property="model.brand.brand"/></td>
			<td><bean:write name="vehicle" property="model.model"/></td>
			<td><bean:write name="vehicle" property="color.color"/></td>
			<td><bean:write name="vehicle" property="fuel.fuel"/></td>
			<td><a href="javascript:editVehicle('<bean:write name="vehicle" property="id"/>');"><img src="resources/editar_tr.gif" border="0" alt="Editar Veiculo"/></a></td>
			<td><a href="javascript:removeVehicle('<bean:write name="vehicle" property="id"/>');"><img src="resources/excluir.gif" border="0" alt="Excluir Veiculo"/></a></td>
			<td>&nbsp;</td>
		</tr>
	
	</logic:iterate>

</table>

</html:form>
